app.controller('AccountRtCtrl', function ($scope, ajax, toaster) {

    $scope.account = { incomes: [], expends: [] };
    $scope.timeRange = {};
    $scope.queryMap = {};
    $scope.stations = [];

    var now = new Date();
    $scope.minMax = [];
    $scope.timeRange.startTime = now.pattern('yyyy/MM/') + '01';
    $scope.timeRange.endTime = now.pattern('yyyy/MM/dd');
    $scope.min = new Date($scope.timeRange.startTime + ' 00:00:00').getTime();
    $scope.max = new Date($scope.timeRange.endTime + ' 23:59:59').getTime();

    $scope.checkQueryMap = function () {
        if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
            var startTime;
            var endTime;
            if (typeof ($scope.timeRange.startTime) == "string") {
                startTime = $scope.timeRange.startTime;
            } else {
                startTime = $scope.timeRange.startTime.pattern("yyyy/MM/dd");
            }
            if (typeof ($scope.timeRange.endTime) == "string") {
                endTime = $scope.timeRange.endTime;
            } else {
                endTime = $scope.timeRange.endTime.pattern("yyyy/MM/dd");
            }
            var s = new Date(startTime).format("yyyy-MM-dd");
            var e = new Date(endTime).format("yyyy-MM-dd");
            $scope.queryMap.timeRange = s + "#@" + e;
            $scope.minMax = [
                new Date(startTime + " 00:00:00").getTime(),
                new Date(endTime + " 23:59:59").getTime()
            ];
        } else {
            $scope.minMax = [];
            delete $scope.queryMap.timeRange;
            toaster.pop('warning', '提示', '请选择开始和结束时间!');
            return false;
        }
        return true;
    };

    $scope.query = function () {
        if (!$scope.checkQueryMap()) return;
        ajax.call(
            new Req.GET({ params: $scope.queryMap, resource: 'account/rt' })
        ).then(function (result) {
            if (result && result.code == 200) {
                $scope.account.incomes = result.result.incomes;
                $scope.account.expends = result.result.expends;
                $scope.parseAccount();
            } else {
                toaster.pop('error', '错误', result.result);
                $scope.account = { incomes: [], expends: [] };
            }
        }, function () {
            $scope.account = { incomes: [], expends: [] };
            toaster.pop('error', '错误', '与服务器的连接失败！');
        });
    };
    $scope.exportIncome = function () {
        if (!$scope.checkQueryMap()) return;
        ajax.call(new Req.GET({
            params: $scope.queryMap,
            resource: 'account/rt/export/income'
        })).then(function (result) {
            if (result && result.code == 200) {
                window.open(result.result);
                toaster.pop('success', '成功', '导出收入报表成功！');
            } else {
                toaster.pop('error', '错误', result.result);
            }
        }, function () {
            toaster.pop('error', '错误', '与服务器的连接失败！');
        });
    };
    $scope.exportExpend = function () {
        if (!$scope.checkQueryMap()) return;
        ajax.call(new Req.GET({
            params: $scope.queryMap,
            resource: 'account/rt/export/expend'
        })).then(function (result) {
            if (result && result.code == 200) {
                window.open(result.result);
                toaster.pop('success', '成功', '导出支出报表成功！');
            } else {
                toaster.pop('error', '错误', result.result);
            }
        }, function () {
            toaster.pop('error', '错误', '与服务器的连接失败！');
        });
    };

    $scope.in = { ali: [[0,0]], wx: [[0,0]], wxh5: [[0,0]], max: 1 };
    $scope.outs = { ali: [[0,0]], wx: [[0,0]], wxh5: [[0,0]], max: 1 };
    $scope.parseAccount = function () {
        if ($scope.minMax.length > 1) {
            $scope.min = $scope.minMax[0];
            $scope.max = $scope.minMax[1];
        }
        $scope.in = { ali: [], wx: [], wxh5: [], max: 1 };
        var _inLen = $scope.account.incomes.length;
        for (var i = 0; i < _inLen; i++) {
            var _in = $scope.account.incomes[i];
            if (_in.income > $scope.in.max) $scope.in.max = _in.income + 500;
            if (_in.payment == "支付宝") {
                $scope.in.ali.push([new Date(_in.day).getTime(), _in.income]);
            } else if (_in.payment == "微信") {
                $scope.in.wx.push([new Date(_in.day).getTime(), _in.income]);
            } else {
                $scope.in.wxh5.push([new Date(_in.day).getTime(), _in.income]);
            }
        }
        if ($scope.in.ali.length == 0) $scope.in.ali.push([0, 0]);
        if ($scope.in.wx.length == 0) $scope.in.wx.push([0, 0]);
        if ($scope.in.wxh5.length == 0) $scope.in.wxh5.push([0, 0]);

        $scope.out = { ali: [], wx: [], wxh5: [], max: 1 };
        var _outLen = $scope.account.expends.length;
        for (var j = 0; j < _outLen; j++) {
            var _out = $scope.account.expends[j];
            if (_out.refund > $scope.out.max) $scope.out.max = _out.refund + 500;
            if (_out.payment == "支付宝") {
                $scope.out.ali.push([new Date(_out.day).getTime(), _out.refund]);
            } else if (_out.payment == "微信") {
                $scope.out.wx.push([new Date(_out.day).getTime(), _out.refund]);
            } else {
                $scope.out.wxh5.push([new Date(_out.day).getTime(), _out.refund]);
            }
        }
        if ($scope.out.ali.length == 0) $scope.out.ali.push([0, 0]);
        if ($scope.out.wx.length == 0) $scope.out.wx.push([0, 0]);
        if ($scope.out.wxh5.length == 0) $scope.out.wxh5.push([0, 0]);
    };

    $scope.query();
});